package sweep;

import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;
import java.io.*;
import java.text.SimpleDateFormat;
import java.awt.event.ActionListener;

public class Client {

    static Socket client;

    private static DataOutputStream out;
    private static DataInputStream in;
    public static String name;
    public Client(String m_name, Socket m_client) {name = m_name; client = m_client;}
    public static void main(String[] args) throws Exception {
        try {
            client = new Socket("127.0.0.1", 8080);
            out = new DataOutputStream(client.getOutputStream());
            in = new DataInputStream(client.getInputStream());
            client.setSoTimeout(100);

        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        Sweep.startLoginBoard(client);
    }
    public static String getMessage() throws IOException {
        try {
            return in.readUTF();
        } catch (IOException e) {
            return "Error";
        }
    }
    public void sendMessage(String msg) {
        try {
            new PrintWriter(
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    client.getOutputStream()
                            )
                    ), true
            ).println(msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void quit() {
        try {
            String msg = "quit";
            out.writeUTF(msg);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
